#include <stdio.h>
#include <stdlib.h>


int size=-1;
int *list;
/////////////////////////////////////
/////To Append numbers in a list/////
/////////////////////////////////////
void appendlist(int num)
        {
        size++;
        list=realloc(list,(size+1)* sizeof(*list));
        list[size]=num;
        
        }
/////////////////////////////////////
/////To Extend numbers in a list/////
/////////////////////////////////////
void extendlist(int *list1,int loc_size)
        {
        int num=0;
        int ch=1;
        list=realloc(list,(size+loc_size+1)*sizeof(*list));
        for(ch=0;ch<loc_size;ch++)
                {
                size++;         
                list[size]=list1[ch];
                }       
        }
/////////////////////////////////////////////////////
/////Inserting a Number in the middle of the list////
/////////////////////////////////////////////////////
void insertlist(int pos)
        {
        int i,num;
        i=num=0;
        if(pos<=size+2)
                {
                printf("Enter the number");
                scanf("%d",&num);
                list=realloc(list,(size+2)*sizeof(*list));      
                for(i=size;i>=(pos-1);i--)
                        list[i+1]=list[i];
                list[pos-1]=num;
                size++;
                }
        else
                printf("ERROR:Invalid Position!!!!!");
        }
/////////////////////////////
///TO DISPLAY THE LIST///////
/////////////////////////////
void displaylist()
        {
        int j;
        printf("\n");
        for(j=0;j<=size;j++)
                {
                printf("%d->",list[j]);
                }
        printf("\n");
        }
////////////////////////////////////////
///TO REMOVE A NUMBER FROM THE LIST/////
////////////////////////////////////////
void removelist(int num)
        {
        int j,flag;
        flag=0;

        for(j=0;j<=size;j++)
                {
                if(list[j]==num)
                        {
                        flag=1;
                        while(j<size)
                                {
                                list[j]=list[j+1];
                                j++;
                                }
                        
                        list=realloc(list,size*sizeof(*list));                  
                        size--;
                        break;
                        }
                }       
        if(flag==0)
                printf("ERROR: NUMBER NOT IN LIST!!!!!\n");
        }
////////////////////////////////////////////////
///TO REMOVE A NUMBER FROM THE GIVEN INDEX//////
////////////////////////////////////////////////
int poplist(int pos)
        {
        int i,element;
        if(pos<=size && pos>=0)
                {
                element=list[pos];
                for(i=pos;i<size;i++)
                        list[i]=list[i+1];
                size--;
                list=realloc(list,size*sizeof(*list));
                return element;
                }
        else
                printf("ERROR:INVALID POSITION!!!!");
        
        
        }
//////////////////////////////////////////////////////////
///////TO KNOW WAT IS THE VALUE AT A GIVEN POSITION///////
//////////////////////////////////////////////////////////
int indexlist(int pos)
        {
        if(pos<=size+1)
                {
                printf("Element at position %d is %d",pos,list[pos-1]);
                return list[pos-1];
                }
        else
                printf("Error!!!INVALID POSITION");
                return -1;
        }

///////////////////////////////////////////
/////TO SORT A LIST ON BASIS OF VALUES/////
///////////IN ASCENDING ORDER//////////////
///////////////////////////////////////////
void sortlist()//Bubble Sort(yet 2 change 2 insertion)
        {
        int temp,i,j;
        i=j=0;
        for(i=0;i<=size;i++)
                {
                for(j=0;j<=size;j++)
                        {
                        if(list[i]<=list[j])
                                {
                                temp=list[i];
                                list[i]=list[j];
                                list[j]=temp;
                                }
                        }
                }
        }
////////////////////////////////////
/////FOR REVERSING A GIVEN LIST/////
////////////////////////////////////
int reverselist()
        {
        int i,j;
        i=j=0;
        for(i=0,j=size;i<j;i++,j--)
                {
                list[i]=list[i]+list[j];
                list[j]=list[i]-list[j];
                list[i]=list[i]-list[j];
                }
        }
//////////////////////////////////////////////////////
////TO FIND COUNT OF A GIVEN NUMBER IN THE LIST///////
//////////////////////////////////////////////////////
int count(int num)
        {
        int i,ctr;
        i=ctr=0;
        for(i=0;i<=size;i++)
                {
                if(list[i]==num)
                        ctr++;
                }
        return ctr;
        }


        
///////////////////////////////////////////////////////
////////////  /\  /\      /\    |   /\    /  //////////
//////////// /  \/  \    /__\   |  /  \  /  ///////////
/////////// /        \  /    \  | /    \/  ////////////
///////////////////////////////////////////////////////
int main(void)
        {
        system("clear");
        int pos=0;
        int choice=1;
        int i=0;
        int *list1;
        printf("Enter no. of elements you would like to Enter in the List");
        scanf("%d",&size);
        size--;
        list=malloc((size+1)* sizeof(*list));
        printf("Enter input\n");
        for(i=0;i<=size;i++)
                {
                scanf("%d",&list[i]);
                }
        while(choice)
                {
                printf("\nNOTE:Every count starts from 1\n");
                printf("1)Append\n2)Extend\n3)Insert\n4)Remove\n5)Poplastelement\n6)Pop\n7)Index\n8)Count\n9)Sort\n10)Reverse\n0)Exit\nYour Choice ");
                scanf(" %d",&choice);
                switch(choice)
                        {
                        case 1:
                                {
                                printf("Enter number to append");
                                scanf("%d",&pos);//Actually its not position(pos) but a number
                                appendlist(pos);
                                break;
                                }
                        case 2:
                                {
                                printf("Enter no. of elements you would like to Extend in the List");
                                scanf("%d",&pos);
                                list1=malloc((pos)* sizeof(*list1));
                                printf("Enter input\n");
                                for(i=0;i<pos;i++)
                                        {
                                        scanf("%d",&list1[i]);
                                        } 
                                extendlist(list1,pos);
                                break;
                                }
                        case 3:
                                {
                                printf("Enter Position");
                                scanf(" %d",&pos);
                                insertlist(pos);
                                break;
                                }
                        case 4:
                                {
                                printf("Enter number to be removed");
                                scanf("%d",&pos);       //pos is a number       
                                removelist(pos);
                                break;
                                }
                        case 5:
                                {
                                pos=poplist(size);//pos represents element poped not position
                                printf("Element Removed %d",pos);
                                break;
                                }
                        case 6:
                                {
                                printf("Enter position to be poped out");
                                scanf("%d",&pos);//this pos is for position
                                pos=poplist(pos-1);//pos on l.h.s. represents element poped not position
                                printf("Element Removed %d",pos);
                                break;
                                }
                        case 7:
                                {
                                printf("Enter Index: ");
                                scanf("%d",&pos);
                                pos=indexlist(pos);     //here pos has the element
                                break;
                                }
                        case 8:
                                {
                                printf("Enter Integer");
                                scanf("%d",&pos);       //here pos is value whose count is to be found
                                pos=count(pos);         //here pos on l.h.s. has the count
                                break;
                                }
                        case 9:
                                {
                                sortlist();
                                break;
                                }
                        case 10:
                                {
                                reverselist();
                                break;
                                }
                      
                        default:
                                {
                                break;
                                }
                        }
                displaylist();
                }
        return 0;
        }  
